package com.example.sudoku

import org.apache.poi.hssf.usermodel.HSSFCellStyle
import org.apache.poi.hssf.usermodel.HSSFFont
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.util.HSSFColor
import org.apache.poi.ss.usermodel.BorderStyle
import org.apache.poi.ss.usermodel.IndexedColors
import org.apache.poi.xssf.usermodel.XSSFCellStyle
import org.apache.poi.xssf.usermodel.XSSFColor
import org.apache.poi.xssf.usermodel.XSSFFont
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import java.awt.Color
import java.io.FileOutputStream
import javax.servlet.ServletOutputStream
import javax.servlet.http.HttpServletResponse

@Controller
@RequestMapping("/sudoku")
class MainController @Autowired constructor(private var sudokuService: SudokuService) {


    @RequestMapping("/index")
    fun index(): String {
        return "index"
    }

    var sudokuQuestion: Array<IntArray>? = null

    @RequestMapping("/create")
    @ResponseBody
    fun createSudoku(): Array<IntArray> {
        val randomList = sudokuService.createNineRandomArray()
        val sudoku = sudokuService.createSudokuArray(randomList)
        sudokuQuestion = sudokuService.createSudokuQuestion(sudoku)
        return sudokuQuestion!!
    }


    // 向右下移多少格
    val downRows = 3
    val rightCols = 3
    val rowIndexArray = arrayOf(1, 2, 3, 7, 8, 9)
    val rowIndexArrayMid = arrayOf(4, 5, 6)
    val cellIndexArrayMid = arrayOf(4, 5, 6)
    val cellIndexArray = arrayOf(1, 2, 3, 7, 8, 9)

    @RequestMapping("/excel")
    fun createExcel(out: ServletOutputStream) {
        if (sudokuQuestion == null) {
            sudokuQuestion = createSudoku()
        }

        // 第一步，workbook，对应一个Excel文件
        val workbook = XSSFWorkbook()
        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        val sheet = workbook.createSheet("sudoku")
        // 统一设置列宽
        sheet.defaultColumnWidth = 7

        // 第三步创建标题的单元格样式titleStyle以及字体样式titleFont
        val cellStyle: XSSFCellStyle = workbook.createCellStyle()
        cellStyle.alignment = HSSFCellStyle.ALIGN_CENTER
        cellStyle.verticalAlignment = HSSFCellStyle.VERTICAL_CENTER
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);

        // 创建字体样式
        val fontStyle: XSSFFont = workbook.createFont()
        // 字体加粗
        fontStyle.boldweight = HSSFFont.BOLDWEIGHT_BOLD
        fontStyle.color = IndexedColors.RED.getIndex()
        fontStyle.fontHeightInPoints = 14
        // 设置字体类型
        fontStyle.fontName = "黑体"
        // 设置字体大小
        fontStyle.fontHeightInPoints = 8.toShort()
        cellStyle.setFont(fontStyle)


        for (i in sudokuQuestion!!.indices) {
            val row = sheet.createRow(i + downRows)
            for (j in sudokuQuestion!![i].indices) {
                val cell = row.createCell(j + rightCols)
                if (rowIndexArray.contains(i + 1)) {
                    if (cellIndexArray.contains(j + 1)) {

                    }
                }

                if (sudokuQuestion!![i][j] == 0) {
                    cell.setCellValue(" ")
                } else {
                    cell.setCellValue(sudokuQuestion!![i][j].toString())
                }

                if (rowIndexArray.contains(i + 1)) {
                    if (cellIndexArray.contains(j + 1)) {
                        cellStyle.fillBackgroundColor = IndexedColors.BLACK.getIndex()
                    } else {
                        cellStyle.fillBackgroundColor = IndexedColors.YELLOW.getIndex()
                    }
                }
                cell.cellStyle = cellStyle

            }
        }
        workbook.write(out)
        out.flush()
        out.close()
    }


}